package com.enqualcomm.kids.mvp.bindphoneaccount;

import android.text.TextUtils;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.mvp.login.wx.view.BindPhoneView;
import com.enqualcomm.kids.network.socket.response.BindUserRelationSetResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;

/**
 * Created by Sunzhigang on 2016/9/14.
 */
public class Presenter implements onBindPhoneAccountListener {

    BindPhoneAccountView view;
    AppDefault appDefault;
    Bindmodel bindmodel;

    public Presenter(BindPhoneAccountView view) {
        this.view = view;
        appDefault = new AppDefault();
        bindmodel = new BindModelImpl();
    }

    public void bindPhoneAccount() {
        if (!TextUtils.isEmpty(view.getAccount()) && !TextUtils.isEmpty(view.getCountryCode()) && !TextUtils.isEmpty(view.getPassword())) {
            bindmodel.bindPhoneAccount(appDefault.getUserid(), appDefault.getUserkey(), appDefault.getWXUserid(),
                    view.getCountryCode(), view.getAccount(), view.getPassword(), this);
        } else if (TextUtils.isEmpty(view.getCountryCode())) {
            view.bindFailed(R.string.login_choice_country);
        } else if (TextUtils.isEmpty(view.getAccount())) {
            view.bindFailed(R.string.input_your_number);
        } else {
            view.bindFailed(R.string.login_input_password);
        }

    }

    @Override
    public void success(LoginOtherResult response) {
        appDefault.setWXLoginType(Integer.parseInt(response.result.type));
        appDefault.setUsername(response.result.username);
        appDefault.setUserid(response.result.userid);
        appDefault.setUserkey(response.result.userkey);
        appDefault.setProducts(response.result.products);
        appDefault.setApiUsername(response.result.username);
        appDefault.setLoginByWX(true);
        new UserDefault(response.result.userid).setLoginOtherResult(response);
        view.bindSuccess(response);
    }

    @Override
    public void failed(String message) {
        view.bindFailed(message);
    }

    @Override
    public void failed(int resID) {
        view.bindFailed(resID);
    }
}
